function defineElectricityWidgets(){
	hf.widget.ElectricityTarifWindow = Ext.extend(hf.widget.TarifWindow, {
		constructor: function(config) {
			hf.widget.ElectricityTarifWindow.superclass.constructor.apply(this, arguments);
		},
		
		prepareSaving: function(){
			var result = true;
			if(this.overSummaField.getValue() < this.underSummaField.getValue()){
				result = false;
				Ext.Msg.alert(labels['failure.alert.title'], messages['message.tarif.under.greater.over']);
			}
			return result;
		}
	});
	Ext.reg('electricity-tarif-window', hf.widget.ElectricityTarifWindow);

	hf.widget.ElectricityWindow = Ext.extend(hf.widget.CommunalWindow, {
		constructor: function(config) {
			hf.widget.ElectricityWindow.superclass.constructor.apply(this, arguments);   
		    this.relayEvents(this.currentValueField, ['change']);
		    this.relayEvents(this.previousValueField, ['change']);
		},
		
		prepareSaving: function(){
			var result = true;
			if(this.currentValueField.getValue() < this.previousValueField.getValue()){
				result = false;
				Ext.Msg.alert(labels['failure.alert.title'], messages['message.electricity.prev.greater.current']);
			}
			return result;
		},
		
		//calculate summa when tarif, curenValue or previousValue changes
		calculateSumma: function(){
			var summa = 0;
			var currentValue = this.currentValueField.getValue();
			var previousValue = this.previousValueField.getValue();
			var delta = currentValue - previousValue;
			var selectTarifId = this.tarifCombo.getValue();
			var tarif = this.tarifCombo.getStore().getById(selectTarifId);
			if(tarif != null){
				if(delta <= tarif.data.borderValue){
					summa = delta*tarif.data.underSumma;
				}else{
					summa = tarif.data.borderValue*tarif.data.underSumma + 
					(delta - tarif.data.borderValue)*tarif.data.overSumma;
				}
			}
			this.summaField.setValue(summa);
		}
	});
	Ext.reg('electricity-window', hf.widget.ElectricityWindow);
};

function createElectricityTab(){
	defineElectricityWidgets();
	
	//store for tarif records
	var tarifStore = {
		xtype: 'app-store',
		viewAction: 'electricityTarif/view.action',
		addAction: 'electricityTarif/update.action',
		updateAction: 'electricityTarif/update.action',
		deleteAction: 'electricityTarif/delete.action',
		columnArray: [
         	{name: indexes['id'], type: INTEGER}, 
         	{name: indexes['enabled'], type: INTEGER},
         	{name: indexes['modelId'],  type: INTEGER},
         	{name: 'tarifName', type: STRING},
         	{name: 'underSumma', type: FLOAT},
         	{name: 'overSumma',  type: FLOAT},
         	{name: 'borderValue',  type: INTEGER}
      	],
      	defaultData: {id : FAKE_ID, enabled : ENTITY_STATE_ENABLED, modelId : FAKE_ID, tarifName: '', underSumma: 0.00, overSumma: 0.00, borderValue: 0}
	};
	
	//store for communal records
	var recordStore = {
		xtype: 'app-store',
		viewAction: 'electricity/view.action',
		addAction: 'electricity/update.action',
		updateAction: 'electricity/update.action',
		deleteAction: 'electricity/delete.action',
		columnArray: [
         	{name: indexes['id'], type: INTEGER}, 
         	{name: indexes['enabled'], type: INTEGER},
         	{name: 'currentValue', type: INTEGER},
         	{name: 'previousValue', type: INTEGER},
         	{name: indexes['summa'], type: FLOAT},
         	{name: indexes['date'],  type: DATE},
         	{name: indexes['modelId'],  type: INTEGER},
         	{name: indexes['month'],  type: INTEGER},
         	{name: indexes['year'],  type: INTEGER},
         	{name: 'tarifId',  type: INTEGER},
         	{name: 'monthYear',  type: STRING}
      	],
      	defaultData: {id : FAKE_ID, enabled : ENTITY_STATE_ENABLED, modelId : FAKE_ID, tarifId: 0, 
			date: new Date(), year: new Date().getFullYear(), month: new Date().getMonth(), 
			currentValue: 0, previousValue: 0, summa: 0.00, monthYear: ''}
	};
	
	var tarifWindow = {
		xtype: 'electricity-tarif-window',
		height: 200,
		formItems: [
			{xtype: 'id-hidden-field'},
			{xtype: 'enabled-hidden-field'},
			{xtype: 'modelid-hidden-field'},
			{xtype: 'textfield', fieldLabel: labels['label.tarif.name'], name: 'tarifName', allowBlank: false, blankText: labels['label.value.empty']},
		    {
		 	 xtype: 'double-number-field', 
		     ref: '../underSummaField', 
		     fieldLabel: labels['label.tarif.under'], 
		     name: 'underSumma', 
		     decimalPrecision: 4,
		     vtype: 'fourSign',
		     minValue: 0.0001, 
		     minText: labels['label.value.positive']
		    },
		    {
		     xtype: 'double-number-field',
		     ref: '../overSummaField',
		     fieldLabel: labels['label.tarif.over'],
		     name: 'overSumma',
		     decimalPrecision: 4,
		     vtype: 'fourSign'
		    },
		    {xtype: 'integer-number-field', fieldLabel: labels['label.tarif.energy.amount'], name: 'borderValue'}
	    ]
	};
	
	var communalWindow = {
		xtype: 'electricity-window',
		height:270,
		formItems: [
			{xtype: 'id-hidden-field'},
			{xtype: 'enabled-hidden-field'},
			{xtype: 'modelid-hidden-field'},
			{xtype: 'tarif-combo', store: tarifStore},
		    {xtype: 'communal-date-field'},
		    {xtype: 'year-combo'},
		    {xtype: 'month-combo'},
		    {xtype: 'integer-number-field', ref: '../currentValueField', fieldLabel: labels['label.electricity.current.value'], name: 'currentValue'},
		    {xtype: 'integer-number-field', ref: '../previousValueField',fieldLabel: labels['label.electricity.previous.value'], name: 'previousValue'},
		    {xtype: 'double-number-field', ref: '../summaField', fieldLabel: labels['label.summa'], name: 'summa', readOnly: true}			    
	    ]
	};

	//grid for displaying tarif data
	var tarifGrid = {
		xtype: 'app-grid',
		ref: '../../tarifGrid',
		store: tarifStore,
   		colModel: new Ext.grid.ColumnModel({
       		defaults: {width: 70, sortable: true, hideable: true},
        	columns: [new Ext.grid.RowNumberer({header: '№'}),
        			  {xtype: 'gridcolumn', id: 'tarifName', dataIndex: 'tarifName', header: labels['label.tarif.name'], width: 100, tooltip: qtips['qtips.column.tarif.name'], hideable: false},
       			 	  {xtype: 'numbercolumn', id: 'underSumma', dataIndex: 'underSumma', header: labels['label.tarif.under'], format: '0.0000', tooltip: qtips['qtips.column.tarif.undersumma']},
       			 	  {xtype: 'numbercolumn', id: 'overSumma', dataIndex: 'overSumma', header: labels['label.tarif.over'], format: '0.0000', tooltip: qtips['qtips.column.tarif.oversumma']},
       			 	  {xtype: 'numbercolumn', id: 'borderValue', dataIndex: 'borderValue', header: labels['label.tarif.energy.amount.column.header'], tooltip: qtips['qtips.column.tarif.bordervalue']},
       			 	  {xtype: 'action-column'}
       		]
 		}),
 		windowType: tarifWindow,
 		autoExpandColumn: 'tarifName',
 		anchor: '100% 40%'
	};
	
	//grid for displaying communal records
	var recordGrid = {
		xtype: 'communal-grid',
		title: 'Записи расходов по электроэнергии',
		ref: '../recordGrid',
		store: recordStore,
   		colModel: new Ext.grid.ColumnModel({
       		defaults: {width: 70, sortable: true, hideable: true},
        	columns: [new Ext.grid.RowNumberer({header: '№'}),
        			  {xtype: 'datecolumn', id: indexes['date'], dataIndex: indexes['date'], header: labels['label.date'], tooltip: qtips['qtips.column.electricity.date']},
       			 	  {xtype: 'gridcolumn', id: indexes['year'], dataIndex: indexes['year'], header: labels['label.year'], tooltip: qtips['qtips.column.year']},
       			 	  {xtype: 'gridcolumn', id: indexes['month'], dataIndex: indexes['month'], header: labels['label.month'], tooltip: qtips['qtips.column.month'],
       			 	  renderer: function(val, x, rec){return months[val][1];}},
       			 	  {xtype: 'numbercolumn', id: 'currentValue', dataIndex: 'currentValue', header: labels['label.electricity.current.value'], tooltip: qtips['qtips.column.electricity.currentvalue'], width: 90},
       			 	  {xtype: 'numbercolumn', id: 'previousValue', dataIndex: 'previousValue', header: labels['label.electricity.previous.value'], tooltip: qtips['qtips.column.electricity.previousvalue'], width: 90},
       			 	  {xtype: 'numbercolumn', id: indexes['summa'], dataIndex: indexes['summa'], header: labels['label.summa'], tooltip: labels['label.summa']},
       			 	  {xtype: 'action-column'}
       		]
 		}),
 		windowType: communalWindow,
 		autoExpandColumn: indexes['summa'],
 		flex: 1
	};
	
	//tab panel
	var tab = {
		xtype: 'app-tab',
		title: labels['label.electricity.tab.title'],
		//iconCls: 'icon-energy',
		items: [{
			layout: {type: 'hbox', align: 'stretch'},
    		anchor: '100%, 100%',
    		items: [recordGrid, {flex: 1, layout: 'anchor', items: [tarifGrid, {xtype: 'chart-panel'}]}]
		}]
	};
	
	return tab;
};
